<?php
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\BankTraits\WYZFBankMap;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class WYZFPayment extends RechargeBaseAbstract implements WAPable
{
    use WYZFBankMap;

    protected $gw_pay = 'http://zf.6r6i.com/apisubmit';
    protected $gw_query = 'http://zf.6r6i.com/apiorderquery';
    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($data['status'] == 1) {
            if ($this->veryCallbackSign($data)) {
                return true;
            }
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr  = 'customerid=' . $data['customerid'] . '&';
        $signStr .= 'status=' . $data['status'] . '&';
        $signStr .= 'sdpayno=' . $data['sdpayno'] . '&';
        $signStr .= 'sdorderno=' . $data['sdorderno'] . '&';
        $signStr .= 'total_fee=' . $data['total_fee'] . '&';
        $signStr .= 'paytype=' . $data['paytype'] . '&';
        $signStr .= $this->getMchKey();
        if ($data['sign'] == md5($signStr)) {
            return true;
        }
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['sdorderno'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['total_fee'];
    }

    public function getThirdNoFromCallback(array $data)
    {
//        return parent::getThirdNoFromCallback($data); // TODO: Change the autogenerated stub
        return $data['sdpayno'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::WYZF;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::ALI_WAP => 'alipaywap'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('customerid', $this->getMchId());
        $this->setParameter('sdorderno', $rechargeOrder->plat_no);
        $this->setParameter('reqtime', date('YmdHis'));
        $this->setParameter('sign', $this->querySign());
        $url = $this->getGwQuery() . '?' . http_build_query($this->getParameters());

        $res = curlHttp($url, [], 'get');
        if ($res['http_code'] == 200) {
            $content = $res['body'];
            try {
                $return = json_decode($content, true);
                if ($return['status'] == 1) {
                    if ( ! $rechargeOrder->third_no) {
                        $rechargeOrder->third_no = $return['sdpayno'];
                        $rechargeOrder->save();
                    }
                    return true;
                }
                return $content;
            } catch (\Exception $exception) {
                return $exception->getMessage() . '||||resBody:'. $res['body'];
            }

        }
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr  = 'version=' . $this->getParameter('version') . '&';
        $signStr .= 'customerid=' . $this->getParameter('customerid') . '&';
        $signStr .= 'total_fee=' . $this->getParameter('total_fee') . '&';
        $signStr .= 'sdorderno=' . $this->getParameter('sdorderno') . '&';
        $signStr .= 'notifyurl=' . $this->getParameter('notifyurl') . '&';
        $signStr .= 'returnurl=' . $this->getParameter('returnurl') . '&';
        $signStr .= $this->getMchKey();
        return md5($signStr);

    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr  = 'customerid=' . $this->getParameter('customerid') . '&';
        $signStr .= 'sdorderno=' . $this->getParameter('sdorderno') . '&';
        $signStr .= 'reqtime=' . $this->getParameter('reqtime') . '&';
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        $mchData = $rechargeOrder->order_data;
        // TODO: Implement initParameters() method.
        $this->setParameter('version', '1.0');
        $this->setParameter('customerid', $this->getMchId());
        $this->setParameter('sdorderno', $rechargeOrder->plat_no);
        $this->setParameter('total_fee', $rechargeOrder->order_amt);
        $this->setParameter('paytype', $this->getPaymentMap($mchData['recharge_type']));
        if ($this->getParameter('paytype') == 'bank') {
            $this->setParameter('bankcode', $this->getBank($mchData['bank_code']));
        }
        $this->setParameter('notifyurl', $this->getCallbackUrl());
        $this->setParameter('returnurl', $this->getReturnUrl());
        $this->setParameter('remark', $this->setRemark($rechargeOrder));
        $this->setParameter('sign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        $url = $this->getGwPay() . '?' . http_build_query($this->getParameters());
        return new HrefRedirectAction($url);
    }
}
